package com.hand.demo.config.valid;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hand.demo.app.service.DemoTestClients;
import com.hand.demo.domain.entity.LmExample;
import org.hzero.boot.imported.app.service.ValidatorHandler;
import org.hzero.boot.imported.infra.validator.annotation.ImportValidator;
import org.hzero.boot.imported.infra.validator.annotation.ImportValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

/**
 *
 */
@ImportValidators({
        @ImportValidator(templateCode = "TEST-ClIENT-44589")
})
public class Validator extends ValidatorHandler {
    @Autowired
    private ObjectMapper objectMapper;

    public static final Logger logger = LoggerFactory.getLogger(DemoTestClients.class);

    @Override
    public boolean validate(String data) {
        LmExample lmExample;
        try {
            lmExample = objectMapper.readValue(data, LmExample.class);
        } catch (IOException e) {
            logger.info(e.getMessage());
            return false;
        }
        if (lmExample.getName().equals("zhangsan")) {
           getContext().addErrorMsg("zhangsan为敏感数据");
           return false;
        }
        return true;
    }
}
